package com.xgc.erp.info.entity.service;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.upload.UploadFile;
import com.xgc.common.base.service.BaseService;
import com.xgc.common.config.Constan;
import com.xgc.common.kit.IdKit;
import com.xgc.common.model.EntityImg;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class EntityImgService extends BaseService {

    private EntityImg dao = new EntityImg().dao();

    @Override
    public Model<?> getDao() {
        return dao;
    }

    public List<EntityImg> getList(String entityCode){
        return dao.find("select * from entity_img where entity_code = ?",entityCode);
    }

    /**
     * 保存文件记录
     *
     * @param list
     *            附件关联对象Id
     * @return
     */
    public List<String> saveFiles(List<UploadFile> list, String vs, String entityCode) {
        List<String> results = new ArrayList<String>();
        List<EntityImg> modelList=new ArrayList<>();
        for (UploadFile uf : list) {
            EntityImg entity=createFileUploaded(uf, vs, entityCode);
            if(entity!=null){
                modelList.add(entity);
                results.add(entity.getImgUrl());
            }
        }
        Db.batchSave(modelList, 50);
        return results;
    }

    /**
     * 创建文件上传文件对象信息
     * @param uploadFile
     * @param objectId
     * @return
     */
    private EntityImg createFileUploaded(UploadFile uploadFile, String objectId, String entityCode){
        if(uploadFile==null) {
            return null;
        }
        // 获取已上传的文件
        File file = uploadFile.getFile();
        String filePath = uploadFile.getUploadPath();
        String fileName = uploadFile.getFileName();
        String subfix = fileName.substring(fileName.lastIndexOf("."));
        long fileSize = file.length();

        // 创建目录
        String rename = IdKit.createFileId();
        String date = rename.substring(0, 8);
        String newFilePath = filePath;
        File f = new File(newFilePath, date);
        if (!f.exists()) {
            f.mkdirs();
        }

        // 文件重命名
        boolean b = file.renameTo(new File(f, rename + subfix));
        if (b) {
            filePath = newFilePath + "/" + date + "/" + rename + subfix;
        } else {
            filePath = filePath + "/" + fileName;
            date = "";
            rename = fileName.substring(0, fileName.indexOf("."));
        }

        // 保存记录
        EntityImg entityImg = new EntityImg();
        Integer imgId = getPrimaryKey(Constan.SEQ_ENTITY_IMG);
        entityImg.setImgId(imgId.longValue());
        entityImg.setImgName(fileName);
        entityImg.setImgUrl(filePath);
        entityImg.setImgSize(fileSize);
        entityImg.setCreateTime(new Date());
        entityImg.setCreator(objectId);
        entityImg.setEntityCode(entityCode);
        entityImg.setUrl(date + "/" + rename);
        return entityImg;
    }


}
